/**
 * 
 */
package ua.kiev.ntukpi.asu.ga.impl;

import java.util.Comparator;
import java.util.List;

import ua.kiev.ntukpi.asu.ga.Individual;
import ua.kiev.ntukpi.asu.ga.Job;
import ua.kiev.ntukpi.asu.ga.interfaces.Fitness;

/**
 * @author Alexander Galibey
 *
 */
public class FitnessImpl implements Fitness {

	private static final String NAME = "Fitness";
	private Comparator<Individual> comparator = new Comparator<Individual>() {

		public int compare(Individual o1, Individual o2) {

			return Double.compare(calculate(o1), calculate(o2));
			
		}

	};

	public Double calculate(Individual individual) {

		int sumOverDeadlines = 0;
		int sumDurations = 0;
		List<Job> jobs = individual.getJobs();
		for (Job job : jobs) {

			sumDurations += job.getDuration();
			int overDeadline = sumDurations - job.getDeadline();
			if (overDeadline > 0) {

				sumOverDeadlines += overDeadline;
			}
		}
		return 1.0 / sumOverDeadlines;
	}

	public Comparator<Individual> getComparator() {

		return this.comparator;
	}

	
	public String getName() {
		
		return NAME;
	}

}
